package com.itheima.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.dubbo.dao.UserInfoDao;
import com.itheima.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {
    @Autowired
    private UserInfoDao userInfoDao;

    //保存用户
    @Override
    public void save(UserInfo userInfo) {
        userInfoDao.insert(userInfo);
    }

    //保存用户头像
    @Override
    public void update(UserInfo userInfo) {
        userInfoDao.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userId) {
        UserInfo userInfo = userInfoDao.selectById(userId);
        return userInfo;
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> lqw = Wrappers.lambdaQuery(UserInfo.class);
        lqw.in(UserInfo::getId, ids);
        if (userInfo != null) {
            if (userInfo.getAge() != null) {
                lqw.lt(UserInfo::getAge, userInfo.getAge());
            }
            if (!StringUtils.isEmpty(userInfo.getGender())) {
                lqw.eq(UserInfo::getGender, userInfo.getGender());
            }
            if (!StringUtils.isEmpty(userInfo.getNickname())) {
                lqw.like(UserInfo::getNickname, userInfo.getNickname());
            }
        }
        List<UserInfo> infos = userInfoDao.selectList(lqw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(infos, "id");
        return map;
    }
}
